<?php

namespace App\Models;

use App\Http\Controllers\Controller;
use App\Http\Controllers\ScoreRuleController;
use Exception;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;

class ScoreLog extends BaseModel
{
    use HasFactory;

    const CREATED_AT = 'create_time';
    const UPDATED_AT = null;

    public $table = 'score_log';





    /**
     * 积分到期清零
     * @return bool
     * @throws Exception
     */
    public function clearScore()
    {
        $clear_time = Cache::get('clear_time');
        if ($clear_time) {
            return false; //已执行，先不执行
        }

        Cache::put('clear_time', 1, 10 * 60); //10分钟执行一次

        $clear_time = '01-07'; //每年1月7号清零
        if (date('m-d') != $clear_time) {
            return false; //今日不能清零
        }
        $date = date('Y-m-d');
        $is_clear = DB::table('score_clear')->where('date', $date)->first();
        if (!empty($is_clear)) {
            return false; //已清零，不能重新清零
        }
        Log::error('开始执行积分到期清零');
        DB::beginTransaction();
        try {
            DB::table('score_clear')->insert(['date' => date('Y-m-d'), 'create_time' => date('Y-m-d H:i:s')]);

            //判断是否执行呢清零操作
            $score_table = config('other.score_table');
            $user_info = DB::table($score_table)->select('id', 'score')->get();
            $obj = new Controller();
            $scoreRuleObj = new ScoreRuleController();
            foreach ($user_info as $key => $val) {
                if (!empty($val->score)) {

                    if ($score_table == 'user_account_lib') {
                        $field_name = 'account_id';
                        $user_id = DB::table('user_info')->where('account_id', $val->id)->value('id');
                        $account_id = $val->id;
                    } else {
                        $field_name = 'user_id';
                        $user_id = $val->id;
                        $account_id = DB::table('user_info')->where('id', $val->id)->value('account_id');
                    }


                    //获取这几天的积分量
                    $score_log_num = DB::table('score_log')->where($field_name, $val->id)->where('create_time', '>=', date('Y-m-01 00:00:00'))->where('create_time', '<=', date('Y-m-07 23:59:59'))->sum('total_score');
                    $clear_score = $val->score - $score_log_num;
                    if ($clear_score < 0) {
                        continue; //积分小于0，不进行清零，积分都是这几天
                    }
                    //用户积分操作  与 系统消息
                    $system_id = $obj->systemAdd("积分到期清零", $user_id, $account_id, 63, null, '积分到期清零: -' . $clear_score . "积分", date('Y-m-d 00:00:00'));

                    $scoreRuleObj->addScoreLog([
                        'user_id' => $user_id,
                        'account_id' => $account_id,
                        'system_id' => $system_id,
                        'score' => $clear_score,
                        'total_score' => $clear_score,
                        'type_id' => 998,
                        'type' => '积分到期清零',
                        'intro' => '积分到期清零: -' . $clear_score . "积分",
                        'create_time' => date('Y-m-d 00:00:00')
                    ]);

                    DB::table($score_table)->where('id', $val->id)->update(['score' => $score_log_num]); //修改剩余积分
                }
            }

            DB::commit();
            Log::error('积分到期清零成功');
            return true;
        } catch (Exception $e) {
            DB::rollBack();
            Log::error('积分到期清零失败');
            Log::error($e->getMessage() . $e->getFile() . $e->getLine());
        }
    }
}
